package com.zy.explore.license.domain.mybatis.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.explore.license.infrastructure.base.mybatis.MpBaseEntity;
import com.zy.explore.license.infrastructure.util.StrUtil;
import com.zy.explore.license.interfaces.frm.CustomerLicenseInfoQueryFrm;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

/**
 * 表(customer_license_info)对应的Mybatis ORM实体对象
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2022年04月06日 10:13:39
 */
@Slf4j
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("customer_license_info")
@Table(name = "customer_license_info")
public class CustomerLicenseInfo extends MpBaseEntity<CustomerLicenseInfo, String> {

    /**
     * 主键
     */
    @Id
    @TableId
    @Column(name = "id")
    private String id;

    /**
     * 客户类型: user-个人(默认), company-公司, institution-机构
     */
    @Column(name = "customer_type")
    private String customerType;

    /**
     * 证书开始生效时间
     */
    @Column(name = "issue_time")
    private Date issueTime;

    /**
     * 客户名称
     */
    @Column(name = "customer_name")
    private String customerName;

    /**
     * 证书到期时间
     */
    @Column(name = "expiry_time")
    private Date expiryTime;

    /**
     * 证书校验自定义参数json串
     */
    @Column(name = "license_check_model")
    private String licenseCheckModel;

    /**
     * 是否删除: 0-否 1-是
     */
    @Column(name = "del_flag")
    private Integer delFlag;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    private Date createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    private Date updateTime;


    /**
     * 组装查询条件
     *
     * @param frm 请求参数
     * @return {@link QueryWrapper<CustomerLicenseInfo> }
     */
    public static QueryWrapper<CustomerLicenseInfo> getWrapper(CustomerLicenseInfoQueryFrm frm) {
        // TODO 请按实际需求组装查询条件
        log.warn("TODO 请按实际需求组装查询条件");
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        QueryWrapper<CustomerLicenseInfo> wrapper = new QueryWrapper<>();
        if(frm.getId() != null) {
            wrapper.eq(StrUtil.fieldToColumn("id"), frm.getId());
        }
        if (!StringUtils.isEmpty(frm.getCustomerType())) {
            wrapper.eq(StrUtil.fieldToColumn("customerType"), frm.getCustomerType());
        }
        if(!StringUtils.isEmpty(frm.getIssueTimeBegin())) {
            wrapper.ge(StrUtil.fieldToColumn("issueTime"), frm.getIssueTimeBegin());
        }
        if(!StringUtils.isEmpty(frm.getIssueTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("issueTime"), frm.getIssueTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getCustomerName())) {
            wrapper.like(StrUtil.fieldToColumn("customerName"), frm.getCustomerName());
        }
        if(!StringUtils.isEmpty(frm.getExpiryTimeBegin())) {
            wrapper.ge(StrUtil.fieldToColumn("expiryTime"), frm.getExpiryTimeBegin());
        }
        if(!StringUtils.isEmpty(frm.getExpiryTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("expiryTime"), frm.getExpiryTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getLicenseCheckModel())) {
            wrapper.eq(StrUtil.fieldToColumn("licenseCheckModel"), frm.getLicenseCheckModel());
        }
        if(frm.getDelFlag() != null) {
            wrapper.eq(StrUtil.fieldToColumn("delFlag"), frm.getDelFlag());
        }
        if(!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
            wrapper.ge(StrUtil.fieldToColumn("createTime"), frm.getCreateTimeBegin());
        }
        if(!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("createTime"), frm.getCreateTimeEnd());
        }
        if(!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
            wrapper.ge(StrUtil.fieldToColumn("updateTime"), frm.getUpdateTimeBegin());
        }
        if(!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("updateTime"), frm.getUpdateTimeEnd());
        }
        return wrapper;
    }
}